package com.yeyaomai.dksns.flowis.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import com.yeyaomai.dksns.flowis.dao.DetailDao;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisQueryOrder;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisStatDetailDTO;
import com.yeyaomai.dksns.flowis.service.FlowisQueryService;
import com.yeyaomai.dksns.flowis.util.NumberFormatUtils;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

/**
 * 
 * @author dingtao@yeyaomai.com
 * 
 */

@Service
public class FlowisQueryServiceImpl implements FlowisQueryService {

	private static Logger logger = LoggerFactory
			.getLogger(FlowisQueryServiceImpl.class);

	@Autowired
	private DetailDao detailDao;

	@Override
	public Page<FlowisStatDetailDTO> getSplit(FlowisQueryOrder order) {

		Date enddate = new Date(); // 现在时间

		Date startdate = null;

		// 今天是 0 昨天 -1 前天 是 -2 以此类推

		int day = NumberFormatUtils.resolveString(order.getDay(), 0);

		startdate = DateUtils.truncate(DateUtils.addDays(enddate, day),
				Calendar.DATE);//截段日期
		if (!order.isPeriod()) // 最近7天 最近 30天
		{
			enddate = DateUtils.addDays(startdate, 1);
		}

		// 如果是 -1 代表 全天查询
		int hour = NumberFormatUtils.resolveString(order.getHour(), -1);

		if (StringUtils.isNotEmpty(order.getHour()) && hour > 0
				&& !order.isPeriod()) {

			startdate = DateUtils.addHours(startdate,
					Integer.valueOf(order.getHour()));

			enddate = DateUtils.addHours(startdate, +1);
		}

		order.setNumber(NumberFormatUtils.resolveString(order.getNumber(), 1)
				+ "");

		System.out.println("startTime---------" + startdate
				+ ",-------------------" + enddate);

		order.setStartTime(VelocityToolDateUtils.formathour.format(startdate));

		order.setEndTime(VelocityToolDateUtils.formathour.format(enddate));

		return detailDao.getSplit(order);
	}

	@Override
	public List<FlowisStatDetailDTO> getDescbyCondition(LinkedHashMap queryArgs) {

		return detailDao.getLastNumberByCondition(queryArgs);

	}

	public void insert(FlowisStatDetailDTO dto) {
		detailDao.insert(dto);
	}

}
